package com.fairytask.ui.item
{
	import com.fairytask.engine.components.button.Button;
	import com.fairytask.engine.components.text.Input;
	import com.fairytask.engine.components.text.Label;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 * 弹窗输入项 
	 * @author Navy
	 */	
	public class InfoItem extends Sprite
	{
		private var _label		:Label;
		private var _input		:Input;
		private var _btn		:Button;
		private var _maxCount	:int	=	0;
		private var _displayAsPassword	:Boolean	=	false;
		
		//数据
		private var _btnWidth	:int	=	0;
		private var _initStr	:String	=	"";
		private var _maxNum	:int	=	1;
		public 	var data		:Array	= 	[];//为外部根据需求传入临时数据
		//回调
		public var callBack	:Function;
		/**
		 * 输入项构造函数
		 * @param _callBack 回调，如果文本改变就调用
		 * @param str Label文本
		 * @param editable 是否可编辑
		 * @param hasMax 是否有最大按钮
		 * @param num 最大值
		 */		
		public function InfoItem(_callBack:Function=null,str:String=null,editable:Boolean=true,hasMax:Boolean=false,num:int=1,pIsPassword:Boolean=false)
		{
			callBack	=	_callBack;
			if(hasMax)
			{
				_initStr	=	_maxNum.toString();
				_maxNum	=	num;
				_btn	=	new Button("",18,18,"Button_34");
				_btnWidth	=	18;
				_btn.y	=	2;
				_btn.x	=	3+150-_btnWidth;
				addChild(_btn);
				_btn.addEventListener(MouseEvent.CLICK, onMaxClick);
			}
			if(str!=""&&str!=null)
			{
				_label	=	new Label();
				_label.htmlStrByBorder	=	str;
				_label.x	=	25;
				addChild(_label);
				
				_input	=	new Input(150-_label.width-_btnWidth,35,"CustSkin_02",12,8);
				addChild(_input);
				_input.displayAsPassword	=	pIsPassword;
				_input.x	=	3 + _label.width + _label.x;
				_input.htmlStr	=	_initStr;
			}
			else
			{
				_input	=	new Input(150-_btnWidth,35,"CustSkin_02",12,8);
				addChild(_input);
				_input.x	=	0;
				_input.htmlStr	=	_initStr;
			}
			if(callBack!=null)
			{
				_input.addEventListener(Event.CHANGE, onTxtChange);
			}
			_input.editable	=	editable;
		}
		public function doCall():void
		{
			if(callBack!=null)
			{
				callBack.call(this,_input.htmlStr);
			}
		}
		private function onTxtChange(evt:Event):void
		{
			callBack.call(this,_input.htmlStr);
		}
		private function onMaxClick(evt:MouseEvent):void
		{
			_input.htmlStr	=	_maxNum.toString();
		}
		public function set text(str:String):void
		{
			_input.htmlStr	=	str;
		}
		public function get text():String
		{
			return _input.htmlStr;
		}
		public function set restrict(str:String):void
		{
			_input.restrict	=	str;
		}

		public function get displayAsPassword():Boolean
		{
			return _displayAsPassword;
		}

		public function set displayAsPassword(value:Boolean):void
		{
			_displayAsPassword = value;
			_input.displayAsPassword	=	value;
		}
		
		public function set maxNum(val:int):void
		{
			_maxNum = val;
		}
		
		public function get maxNum():int
		{
			return _maxNum;
		}
		
		public function set maxCharNum(val:int):void
		{
			_input.maxChars = val;
		}
		
		public function set editable(value:Boolean):void
		{
			_input.editable	=	value;		
		}

	}
}